package com.eastfair.projectcore.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.annotation.model.LoadService;
import com.eastfair.core.snowflake.DefaultUidGenerator;
import com.eastfair.core.utils.CollHelper;
import com.eastfair.projectcore.dao.DictionaryMapper;
import com.eastfair.projectcore.entity.Dictionary;
import com.eastfair.projectcore.entity.Menu;
import com.eastfair.projectcore.service.DictionaryService;
import com.eastfair.boot.service.SuperServiceImpl;

import com.eastfair.questionnaire.entity.Questionnaire;
import com.eastfair.uid.model.SelfUidDto;
import com.eastfair.uid.service.SelfUidService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;

import java.io.Serializable;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 字典项
 * </p>
 *
 * @author ligang
 * @date 2021-11-01
 */
@Slf4j
@Service
public class DictionaryServiceImpl extends SuperServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {
//    @Autowired
//    SelfUidService selfUidService;

    /**
     * 处理新增相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSave(Dictionary model) {
        //雪花ID,不需要分库分表
//        SelfUidDto selfUidDto = new SelfUidDto();
//        model.setId(selfUidService.getUid(selfUidDto));
        model.setId(DefaultUidGenerator.getUid(0L));
        if (model.getSequence() == null) {
            //默认排序
            model.setSequence(querySort(model.getType()));
        }
        return R.successDef();
    }

    public int querySort(String type) {
        if (StringUtils.isBlank(type)) {
            return 1;
        }
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Dictionary::getIsDeleted, BusinessConstant.DELETE_NO);
        queryWrapper.lambda().eq(Dictionary::getType, type);
        //查询用户
        List<Dictionary> dictionaryList = list(queryWrapper);
        if (dictionaryList != null && !dictionaryList.isEmpty()) {
            int max = dictionaryList.stream().mapToInt(s -> s.getSequence()).max().getAsInt();
            return max + 1;
        }
        return 1;
    }

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<Dictionary> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }


    @Override
    public List<Dictionary> queryDict(String type) {
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Dictionary::getIsDeleted, BusinessConstant.DELETE_NO);
        queryWrapper.lambda().eq(Dictionary::getType, type);
        List<Dictionary> dictionaryList = list(queryWrapper);
        return dictionaryList;
    }

    @Override
    public List<Dictionary> queryDictGroupType() {
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Dictionary::getIsDeleted, BusinessConstant.DELETE_NO);
        queryWrapper.groupBy("label", "type").select("label", "type");
        List<Dictionary> dictionaryList = list(queryWrapper);
        return dictionaryList;
    }

    @Override
    public Map<Serializable, Object> findNameByIds(Set<Serializable> set) {
        return null;
    }

    @Override
    public Map<Serializable, Object> findByIds(Set<Serializable> codes) {
        if (codes.isEmpty()) {
            return Collections.emptyMap();
        }
        List<Dictionary> dictionaryList = codes.stream().map(code -> {
            QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(Dictionary::getIsDeleted, BusinessConstant.DELETE_NO)
                    .eq(Dictionary::getCode, code);

            return getOne(queryWrapper);
        }).collect(Collectors.toList());

        return CollHelper.uniqueIndex(dictionaryList, Dictionary::getCode, Dictionary::getName);
    }
}
